package com.hashcoding.eyes.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.hashcoding.eyes.R;
import com.hashcoding.eyes.data.entity.PhotosList;
import com.hashcoding.eyes.data.entity.User;
import com.hashcoding.eyes.ui.adapter.PhotosListAdapter;
import com.hashcoding.eyes.ui.widget.NoScrollListView;
import com.hashcoding.eyes.ui.widget.ObservableScrollView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import jp.wasabeef.glide.transformations.CropCircleTransformation;

public class UserActivity extends BaseActivity implements ObservableScrollView
        .OnScrollChangedListener {

    private final static String KEY_USER = "user";

    @Bind(R.id.user_background)
    ImageView mUserBackground;
    @Bind(R.id.user_name)
    TextView mUserName;
    @Bind(R.id.user_description)
    TextView mUserDescription;
    @Bind(R.id.user_avatar)
    ImageView mUserAvatar;
    @Bind(R.id.scrollView)
    ObservableScrollView mScrollView;
    @Bind(R.id.toolbar_bg_green)
    ImageView mToolbarBgGreen;
    @Bind(R.id.star_into)
    TextView mStarInto;
    @Bind(R.id.list_view)
    NoScrollListView mListView;
    @Bind(R.id.add_photo)
    ImageView mAddPhoto;

    private User mUser;
    private PhotosListAdapter mAdapter;

    public static void launch(Context context, User user) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(KEY_USER, user);

        Intent intent = new Intent(context, UserActivity.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        ButterKnife.bind(this);

        mUser = getIntent().getParcelableExtra(KEY_USER);

        initView();
    }

    private void initView() {

        mToolbarBgGreen.setAlpha(0f);
        mScrollView.setOnScrollChangedListener(this);

        Glide.with(this).load(mUser.backgroundURL)
                .placeholder(R.color.default_image).into(mUserBackground);
        Glide.with(this).load(mUser.avatarURL).placeholder(R.drawable.head)
                .bitmapTransform(new CenterCrop(this), new CropCircleTransformation(this))
                .into(mUserAvatar);
        mUserName.setText(mUser.name);
        mUserDescription.setText(mUser.description);

        mAdapter = new PhotosListAdapter(this);
        mListView.setAdapter(mAdapter);
        mListView.setFocusable(false);
        mAdapter.setList(PhotosList.instance().getList());

        mStarInto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FavoritedActivity.launch(UserActivity.this, mUser);
                shortToast("收藏");
            }
        });

        mAddPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddActivity.launch(UserActivity.this);
            }
        });
    }

    @OnClick(R.id.title_left_btn)
    protected void onTitleLeftClick() {
        onBackPressed();
    }

    @Override
    public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {
        float ratio = mScrollView.getScrollY() * 1.0f /
                (mUserBackground.getHeight() - mToolbarBgGreen.getHeight());
        if (ratio < 1) {
            mToolbarBgGreen.setAlpha(ratio);
        } else {
            mToolbarBgGreen.setAlpha(1f);
        }
    }

}
